package com.atguigu.blog.controller;

import com.atguigu.blog.pojo.User;
import com.atguigu.blog.service.UserService;
import com.atguigu.blog.utils.Result;
import com.atguigu.blog.utils.ResultCodeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.lang.model.element.Name;
import java.util.Map;

/**
 * @author MikeCoder
 * @create 2023-11-2516:46
 * @description:
 * @verson:
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
@Tag(name = "用户接口", description = "有关用户的接口")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping
    @Operation(summary = "登录", description = "用户登录功能")
    @Parameters(@Parameter(name = "user", description = "前端传入的用户对象"))
    public Result<Map<String, Object>> login(@RequestBody User user) {
        Map<String, Object> map = userService.login(user);
        if (map != null) {
            return Result.ok(map);
        }
        return Result.fail(map);
    }
    @GetMapping("/{uid}")
    @Operation(summary = "查找用户",description = "通过uid查找用户")
    @Parameters(@Parameter(name = "uid",description = "用户id"))
    public Result<User> getUserByUid(@PathVariable Long uid){
        User user = userService.getUserByUid(uid);
        return Result.ok(user);
    }

    @PutMapping
    @Operation(summary = "修改用户信息",description = "修改用户电话和介绍")
    public Result change(@RequestBody User user){
        Integer result = userService.change(user);
        if(result>0){
            return Result.ok(null);
        }
        return Result.fail(null);
    }
}
